package com.jinyuan.view.circlemenu;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;

public class MenuItemView extends RelativeLayout {

	private Context context;
	
	public MenuItemView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
	}

	public MenuItemView(Context context) {
		super(context);
		this.context = context;
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		if (CircleAnimations.radius == 0)
			throw new RuntimeException("RadiusUnknow!");

		if (changed) {
			int count = getChildCount();
			double angle = CircleAnimations.sector / 8;
			for (int i = 0; i < count; i++) {
				View childView = getChildAt(i);
				childView.setVisibility(View.GONE);
				int width = childView.getMeasuredWidth();
				int height = childView.getMeasuredHeight();
				int x = (int) (getMeasuredWidth()/2 + Math.cos(angle * (9 - 2 * (i + 1))) * CircleAnimations.radius - width / 2);
				int y = (int) (getMeasuredHeight() - CircleAnimations.dip2px(context, 50) - Math.sin(angle * (9 - 2 * (i + 1))) * CircleAnimations.radius);
				childView.layout(x, y, x + width, y + height);
			}
		}
	}

}
